{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy import signal\n",
    "%matplotlib inline  \n",
    "\n",
    "data = pd.read_csv('data.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def RSI(dataframe, period):\n",
    "    '''\n",
    "    Computes the RSI of a given price series for a given period length\n",
    "    :param dataframe:\n",
    "    :param period:\n",
    "    :return dataframe with rsi:\n",
    "    '''\n",
    "\n",
    "    rsi = []\n",
    "\n",
    "    for stock in dataframe['Symbol'].unique():\n",
    "        all_prices = dataframe[dataframe['Symbol'] == stock]['Close']\n",
    "        diff = np.diff(all_prices) # length is 1 less than the all_prices\n",
    "        for i in range(period):\n",
    "            rsi.append(None) # because RSI can't be calculated until period prices have occured\n",
    "\n",
    "        for i in range(len(diff) - period + 1):\n",
    "            avg_gain = diff[i:period + i]\n",
    "            avg_loss = diff[i:period + i]\n",
    "            avg_gain = abs(sum(avg_gain[avg_gain >= 0]) / period)\n",
    "            avg_loss = abs(sum(avg_loss[avg_loss < 0]) / period)\n",
    "            if avg_loss == 0:\n",
    "                rsi.append(100)\n",
    "            elif avg_gain == 0:\n",
    "                rsi.append(0)\n",
    "            else:\n",
    "                rs = avg_gain / avg_loss\n",
    "                rsi.append(100 - (100 / (1 + rs)))\n",
    "\n",
    "    dataframe['RSI'] = rsi\n",
    "    return dataframe\n",
    "\n",
    "\n",
    "def PROC(dataframe, period):\n",
    "    '''\n",
    "    Computes the PROC(price rate of change) of a given price series for a given period length\n",
    "    :param dataframe:\n",
    "    :param period:\n",
    "    :return proc:\n",
    "    '''\n",
    "\n",
    "    proc = []\n",
    "\n",
    "    for stock in dataframe['Symbol'].unique():\n",
    "        all_prices = list(dataframe[dataframe['Symbol'] == stock]['Close'])\n",
    "        for i in range(period):\n",
    "            proc.append(None) # because proc can't be calculated until period prices have occured\n",
    "        for i in range(len(all_prices) - period):\n",
    "            if len(all_prices) <= period:\n",
    "                proc.append(None)\n",
    "            else:\n",
    "                calculated = (all_prices[i + period] - all_prices[i]) / all_prices[i]\n",
    "                proc.append(calculated)\n",
    "    dataframe['PROC'] = proc\n",
    "    return dataframe\n",
    "\n",
    "\n",
    "def SO(dataframe, period):\n",
    "    \n",
    "    so = []\n",
    "    \n",
    "    for stock in dataframe['Symbol'].unique():\n",
    "        all_prices = list(dataframe[dataframe['Symbol'] == stock]['Close'])\n",
    "        \n",
    "        for i in range(period):\n",
    "            so.append(None)\n",
    " \n",
    "        for i in range(len(all_prices) - period):\n",
    "            C = all_prices[i]\n",
    "            H = max(all_prices[i:i+period])\n",
    "            L = min(all_prices[i:i+period])\n",
    "            so.append(100 * ((C - L) / (H - L)))\n",
    "\n",
    "    dataframe['SO'] = so\n",
    "    return dataframe\n",
    "\n",
    "\n",
    "def Williams_R(dataframe, period):\n",
    "    '''\n",
    "    Williams %R\n",
    "    Calculates fancy shit for late usage. Nice!\n",
    "\n",
    "    EXAMPLE USAGE:\n",
    "    data = pandas.read_csv(\"./data/ALL.csv\", sep=\",\",header=0,quotechar='\"')\n",
    "    wr = Williams_R(data)\n",
    "    print(wr)\n",
    "\n",
    "    '''\n",
    "    \n",
    "    wr = []\n",
    "    \n",
    "    for stock in dataframe['Symbol'].unique():\n",
    "        all_prices = list(dataframe[dataframe['Symbol'] == stock]['Close'])\n",
    "        for i in range(period):\n",
    "            wr.append(None) # because proc can't be calculated until period prices have occured\n",
    "            \n",
    "        for i in range(period-1,len(all_prices)-1):\n",
    "            C = all_prices[i]\n",
    "            H = max(all_prices[i-period+1:i])\n",
    "            L = min(all_prices[i-period+1:i])\n",
    "            wr_one = (\n",
    "                ((H - C) \n",
    "                 / (H - L)) * -100\n",
    "            )\n",
    "            if wr_one <=-100:\n",
    "                wr.append(-100)\n",
    "            elif wr_one >= 100:\n",
    "                wr.append(100)\n",
    "            else:\n",
    "                wr.append(wr_one)\n",
    "    dataframe[\"WR\"] = wr\n",
    "    return dataframe\n",
    "\n",
    "\n",
    "def calculate_targets(df, period):\n",
    "    \n",
    "    targets = []\n",
    "\n",
    "    for stock in df['Symbol'].unique():\n",
    "        all_prices = list(df[df['Symbol'] == stock]['Close'])\n",
    "        \n",
    "        for i in range(0, len(all_prices)-period):\n",
    "            targets.append(np.sign(all_prices[i+period] - all_prices[i]))\n",
    "        for i in range(len(all_prices)-period, len(all_prices)):\n",
    "            targets.append(None)\n",
    "\n",
    "    df[\"Target({})\".format(period)] = targets\n",
    "    return df\n",
    "\n",
    "\n",
    "def On_Balance_Volume(dataframe):\n",
    "    '''\n",
    "    Williams %R\n",
    "    Calculates fancy shit for late usage. Nice!\n",
    "\n",
    "    EXAMPLE USAGE:\n",
    "    data = pandas.read_csv(\"./data/ALL.csv\", sep=\",\",header=0,quotechar='\"')\n",
    "    wr = Williams_R(data)\n",
    "    print(wr)\n",
    "\n",
    "    '''\n",
    "    obv = []\n",
    "    \n",
    "    for stock in dataframe['Symbol'].unique():\n",
    "        all_prices = list(dataframe[dataframe['Symbol'] == stock]['Close'])\n",
    "        all_volumes = list(dataframe[dataframe['Symbol'] == stock]['Volume'])\n",
    "    \n",
    "        obv.append(dataframe.iloc[0][\"Volume\"])\n",
    "        for i in range(1,len(all_prices)):\n",
    "            C_old = all_prices[i-1]\n",
    "            C = all_prices[i]\n",
    "            if(C > C_old):\n",
    "                obv.append(obv[i-1]+ all_volumes[i])\n",
    "            elif (C < C_old):\n",
    "                obv.append(obv[i - 1] - all_volumes[i])\n",
    "            else:\n",
    "                obv.append(obv[i-1])\n",
    "\n",
    "    dataframe['OBV'] = obv\n",
    "    return dataframe\n",
    "\n",
    "\n",
    "def delete_bad_data(df):\n",
    "    for stock in df['Symbol'].unique():\n",
    "        if df[df[\"Symbol\"]==stock][\"Close\"].any() < 1:\n",
    "            df = df.drop(df[df[\"Symbol\"]==stock], axis=0)\n",
    "        if df[df[\"Symbol\"]==stock][\"Volume\"].any() == 0:\n",
    "            df = df.drop(df[df[\"Symbol\"]==stock], axis=0)\n",
    "        if df[df[\"Symbol\"]==stock][\"Open\"].any() == None:\n",
    "            df = df.drop(df[df[\"Symbol\"]==stock], axis=0)\n",
    "    return df\n",
    "\n",
    "def detrend_data(df):\n",
    "    trend = None\n",
    "    for stock in df['Symbol'].unique():\n",
    "        all_prices = list(df[df['Symbol'] == stock]['Close'])\n",
    "#        trend.append(signal.detrend(all_prices))\n",
    "        if(trend is None):\n",
    "            trend = list(signal.detrend(all_prices))\n",
    "        else:\n",
    "            trend.extend(signal.detrend(all_prices))\n",
    "        print(\"len(trend):{} len(df['Symbol']):{}\".format(len(trend),len(all_prices)))\n",
    "\n",
    "    print(\"len(trend):{} len(df):{}\".format(len(trend),len(df)))\n",
    "    df['Close_detrend'] = trend\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test of deleting bad data:\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "data = delete_bad_data(data)\n",
    "print(\"Test of deleting bad data:\")\n",
    "try:\n",
    "    print(\"Deletion successful:\", data[data[\"Symbol\"]==\"AAP\"][\"Close\"][5] )\n",
    "except:\n",
    "    print(\"True\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = RSI(data,14)\n",
    "print(\"RSI: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = PROC(data, 14)\n",
    "print(\"PROC: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = SO(data,14)\n",
    "print(\"SO: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = Williams_R(data, 14 )\n",
    "print(\"Williams_R: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data[\"EWMA\"] = pd.ewma(data[\"Close\"], com=.5)\n",
    "print(\"EWMA: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "len(trend):1867 len(df['Symbol']):1867\n",
      "len(trend):2744 len(df['Symbol']):877\n",
      "len(trend):4278 len(df['Symbol']):1534\n",
      "len(trend):6144 len(df['Symbol']):1866\n",
      "len(trend):7272 len(df['Symbol']):1128\n",
      "len(trend):9139 len(df['Symbol']):1867\n",
      "len(trend):11006 len(df['Symbol']):1867\n",
      "len(trend):12872 len(df['Symbol']):1866\n",
      "len(trend):14173 len(df['Symbol']):1301\n",
      "len(trend):16039 len(df['Symbol']):1866\n",
      "len(trend):17906 len(df['Symbol']):1867\n",
      "len(trend):19772 len(df['Symbol']):1866\n",
      "len(trend):21639 len(df['Symbol']):1867\n",
      "len(trend):23267 len(df['Symbol']):1628\n",
      "len(trend):25134 len(df['Symbol']):1867\n",
      "len(trend):27001 len(df['Symbol']):1867\n",
      "len(trend):28868 len(df['Symbol']):1867\n",
      "len(trend):29793 len(df['Symbol']):925\n",
      "len(trend):31660 len(df['Symbol']):1867\n",
      "len(trend):33527 len(df['Symbol']):1867\n",
      "len(trend):35394 len(df['Symbol']):1867\n",
      "len(trend):37261 len(df['Symbol']):1867\n",
      "len(trend):39127 len(df['Symbol']):1866\n",
      "len(trend):40994 len(df['Symbol']):1867\n",
      "len(trend):42622 len(df['Symbol']):1628\n",
      "len(trend):44489 len(df['Symbol']):1867\n",
      "len(trend):45380 len(df['Symbol']):891\n",
      "len(trend):47246 len(df['Symbol']):1866\n",
      "len(trend):49111 len(df['Symbol']):1865\n",
      "len(trend):50976 len(df['Symbol']):1865\n",
      "len(trend):52843 len(df['Symbol']):1867\n",
      "len(trend):54709 len(df['Symbol']):1866\n",
      "len(trend):56337 len(df['Symbol']):1628\n",
      "len(trend):56841 len(df['Symbol']):504\n",
      "len(trend):58707 len(df['Symbol']):1866\n",
      "len(trend):60574 len(df['Symbol']):1867\n",
      "len(trend):62441 len(df['Symbol']):1867\n",
      "len(trend):63583 len(df['Symbol']):1142\n",
      "len(trend):65450 len(df['Symbol']):1867\n",
      "len(trend):67317 len(df['Symbol']):1867\n",
      "len(trend):68382 len(df['Symbol']):1065\n",
      "len(trend):70249 len(df['Symbol']):1867\n",
      "len(trend):72116 len(df['Symbol']):1867\n",
      "len(trend):73982 len(df['Symbol']):1866\n",
      "len(trend):75849 len(df['Symbol']):1867\n",
      "len(trend):76187 len(df['Symbol']):338\n",
      "len(trend):78054 len(df['Symbol']):1867\n",
      "len(trend):79921 len(df['Symbol']):1867\n",
      "len(trend):81788 len(df['Symbol']):1867\n",
      "len(trend):83655 len(df['Symbol']):1867\n",
      "len(trend):85522 len(df['Symbol']):1867\n",
      "len(trend):87389 len(df['Symbol']):1867\n",
      "len(trend):89256 len(df['Symbol']):1867\n",
      "len(trend):91122 len(df['Symbol']):1866\n",
      "len(trend):92989 len(df['Symbol']):1867\n",
      "len(trend):94617 len(df['Symbol']):1628\n",
      "len(trend):96484 len(df['Symbol']):1867\n",
      "len(trend):98351 len(df['Symbol']):1867\n",
      "len(trend):100218 len(df['Symbol']):1867\n",
      "len(trend):102085 len(df['Symbol']):1867\n",
      "len(trend):103952 len(df['Symbol']):1867\n",
      "len(trend):105818 len(df['Symbol']):1866\n",
      "len(trend):107685 len(df['Symbol']):1867\n",
      "len(trend):109552 len(df['Symbol']):1867\n",
      "len(trend):111419 len(df['Symbol']):1867\n",
      "len(trend):111774 len(df['Symbol']):355\n",
      "len(trend):113641 len(df['Symbol']):1867\n",
      "len(trend):115269 len(df['Symbol']):1628\n",
      "len(trend):117136 len(df['Symbol']):1867\n",
      "len(trend):118764 len(df['Symbol']):1628\n",
      "len(trend):120631 len(df['Symbol']):1867\n",
      "len(trend):122497 len(df['Symbol']):1866\n",
      "len(trend):124364 len(df['Symbol']):1867\n",
      "len(trend):126231 len(df['Symbol']):1867\n",
      "len(trend):128098 len(df['Symbol']):1867\n",
      "len(trend):129965 len(df['Symbol']):1867\n",
      "len(trend):131832 len(df['Symbol']):1867\n",
      "len(trend):133587 len(df['Symbol']):1755\n",
      "len(trend):134745 len(df['Symbol']):1158\n",
      "len(trend):135365 len(df['Symbol']):620\n",
      "len(trend):136993 len(df['Symbol']):1628\n",
      "len(trend):138859 len(df['Symbol']):1866\n",
      "len(trend):140725 len(df['Symbol']):1866\n",
      "len(trend):142592 len(df['Symbol']):1867\n",
      "len(trend):143270 len(df['Symbol']):678\n",
      "len(trend):145137 len(df['Symbol']):1867\n",
      "len(trend):147004 len(df['Symbol']):1867\n",
      "len(trend):148870 len(df['Symbol']):1866\n",
      "len(trend):149133 len(df['Symbol']):263\n",
      "len(trend):151000 len(df['Symbol']):1867\n",
      "len(trend):152866 len(df['Symbol']):1866\n",
      "len(trend):154733 len(df['Symbol']):1867\n",
      "len(trend):156600 len(df['Symbol']):1867\n",
      "len(trend):158467 len(df['Symbol']):1867\n",
      "len(trend):160333 len(df['Symbol']):1866\n",
      "len(trend):162199 len(df['Symbol']):1866\n",
      "len(trend):162237 len(df['Symbol']):38\n",
      "len(trend):164104 len(df['Symbol']):1867\n",
      "len(trend):165731 len(df['Symbol']):1627\n",
      "len(trend):167357 len(df['Symbol']):1626\n",
      "len(trend):169224 len(df['Symbol']):1867\n",
      "len(trend):170438 len(df['Symbol']):1214\n",
      "len(trend):172305 len(df['Symbol']):1867\n",
      "len(trend):174172 len(df['Symbol']):1867\n",
      "len(trend):176039 len(df['Symbol']):1867\n",
      "len(trend):177905 len(df['Symbol']):1866\n",
      "len(trend):178906 len(df['Symbol']):1001\n",
      "len(trend):180773 len(df['Symbol']):1867\n",
      "len(trend):182639 len(df['Symbol']):1866\n",
      "len(trend):183028 len(df['Symbol']):389\n",
      "len(trend):184894 len(df['Symbol']):1866\n",
      "len(trend):186761 len(df['Symbol']):1867\n",
      "len(trend):188627 len(df['Symbol']):1866\n",
      "len(trend):190493 len(df['Symbol']):1866\n",
      "len(trend):192360 len(df['Symbol']):1867\n",
      "len(trend):193463 len(df['Symbol']):1103\n",
      "len(trend):195330 len(df['Symbol']):1867\n",
      "len(trend):197197 len(df['Symbol']):1867\n",
      "len(trend):199064 len(df['Symbol']):1867\n",
      "len(trend):200931 len(df['Symbol']):1867\n",
      "len(trend):202798 len(df['Symbol']):1867\n",
      "len(trend):204665 len(df['Symbol']):1867\n",
      "len(trend):206532 len(df['Symbol']):1867\n",
      "len(trend):208399 len(df['Symbol']):1867\n",
      "len(trend):209221 len(df['Symbol']):822\n",
      "len(trend):211087 len(df['Symbol']):1866\n",
      "len(trend):212953 len(df['Symbol']):1866\n",
      "len(trend):214819 len(df['Symbol']):1866\n",
      "len(trend):216212 len(df['Symbol']):1393\n",
      "len(trend):217840 len(df['Symbol']):1628\n",
      "len(trend):219706 len(df['Symbol']):1866\n",
      "len(trend):221573 len(df['Symbol']):1867\n",
      "len(trend):223440 len(df['Symbol']):1867\n",
      "len(trend):225307 len(df['Symbol']):1867\n",
      "len(trend):227174 len(df['Symbol']):1867\n",
      "len(trend):229041 len(df['Symbol']):1867\n",
      "len(trend):230908 len(df['Symbol']):1867\n",
      "len(trend):232775 len(df['Symbol']):1867\n",
      "len(trend):234642 len(df['Symbol']):1867\n",
      "len(trend):236178 len(df['Symbol']):1536\n",
      "len(trend):238044 len(df['Symbol']):1866\n",
      "len(trend):239910 len(df['Symbol']):1866\n",
      "len(trend):241777 len(df['Symbol']):1867\n",
      "len(trend):243644 len(df['Symbol']):1867\n",
      "len(trend):245273 len(df['Symbol']):1629\n",
      "len(trend):247140 len(df['Symbol']):1867\n",
      "len(trend):249007 len(df['Symbol']):1867\n",
      "len(trend):250874 len(df['Symbol']):1867\n",
      "len(trend):252502 len(df['Symbol']):1628\n",
      "len(trend):254369 len(df['Symbol']):1867\n",
      "len(trend):254963 len(df['Symbol']):594\n",
      "len(trend):256830 len(df['Symbol']):1867\n",
      "len(trend):258068 len(df['Symbol']):1238\n",
      "len(trend):259935 len(df['Symbol']):1867\n",
      "len(trend):261801 len(df['Symbol']):1866\n",
      "len(trend):263668 len(df['Symbol']):1867\n",
      "len(trend):265534 len(df['Symbol']):1866\n",
      "len(trend):266666 len(df['Symbol']):1132\n",
      "len(trend):268533 len(df['Symbol']):1867\n",
      "len(trend):268658 len(df['Symbol']):125\n",
      "len(trend):270525 len(df['Symbol']):1867\n",
      "len(trend):272391 len(df['Symbol']):1866\n",
      "len(trend):274257 len(df['Symbol']):1866\n",
      "len(trend):276124 len(df['Symbol']):1867\n",
      "len(trend):277991 len(df['Symbol']):1867\n",
      "len(trend):279857 len(df['Symbol']):1866\n",
      "len(trend):281126 len(df['Symbol']):1269\n",
      "len(trend):282563 len(df['Symbol']):1437\n",
      "len(trend):284430 len(df['Symbol']):1867\n",
      "len(trend):286297 len(df['Symbol']):1867\n",
      "len(trend):288164 len(df['Symbol']):1867\n",
      "len(trend):290030 len(df['Symbol']):1866\n",
      "len(trend):291897 len(df['Symbol']):1867\n",
      "len(trend):293763 len(df['Symbol']):1866\n",
      "len(trend):295629 len(df['Symbol']):1866\n",
      "len(trend):297496 len(df['Symbol']):1867\n",
      "len(trend):299362 len(df['Symbol']):1866\n",
      "len(trend):301229 len(df['Symbol']):1867\n",
      "len(trend):303096 len(df['Symbol']):1867\n",
      "len(trend):304963 len(df['Symbol']):1867\n",
      "len(trend):306829 len(df['Symbol']):1866\n",
      "len(trend):308695 len(df['Symbol']):1866\n",
      "len(trend):310562 len(df['Symbol']):1867\n",
      "len(trend):310658 len(df['Symbol']):96\n",
      "len(trend):310903 len(df['Symbol']):245\n",
      "len(trend):312770 len(df['Symbol']):1867\n",
      "len(trend):314637 len(df['Symbol']):1867\n",
      "len(trend):316462 len(df['Symbol']):1825\n",
      "len(trend):318328 len(df['Symbol']):1866\n",
      "len(trend):319973 len(df['Symbol']):1645\n",
      "len(trend):321839 len(df['Symbol']):1866\n",
      "len(trend):323705 len(df['Symbol']):1866\n",
      "len(trend):323963 len(df['Symbol']):258\n",
      "len(trend):325830 len(df['Symbol']):1867\n",
      "len(trend):327697 len(df['Symbol']):1867\n",
      "len(trend):329563 len(df['Symbol']):1866\n",
      "len(trend):331430 len(df['Symbol']):1867\n",
      "len(trend):333288 len(df['Symbol']):1858\n",
      "len(trend):335155 len(df['Symbol']):1867\n",
      "len(trend):336784 len(df['Symbol']):1629\n",
      "len(trend):338650 len(df['Symbol']):1866\n",
      "len(trend):340517 len(df['Symbol']):1867\n",
      "len(trend):342086 len(df['Symbol']):1569\n",
      "len(trend):343953 len(df['Symbol']):1867\n",
      "len(trend):345820 len(df['Symbol']):1867\n",
      "len(trend):347687 len(df['Symbol']):1867\n",
      "len(trend):349554 len(df['Symbol']):1867\n",
      "len(trend):351421 len(df['Symbol']):1867\n",
      "len(trend):352166 len(df['Symbol']):745\n",
      "len(trend):354032 len(df['Symbol']):1866\n",
      "len(trend):355899 len(df['Symbol']):1867\n",
      "len(trend):357766 len(df['Symbol']):1867\n",
      "len(trend):358175 len(df['Symbol']):409\n",
      "len(trend):360042 len(df['Symbol']):1867\n",
      "len(trend):361909 len(df['Symbol']):1867\n",
      "len(trend):363776 len(df['Symbol']):1867\n",
      "len(trend):365643 len(df['Symbol']):1867\n",
      "len(trend):367509 len(df['Symbol']):1866\n",
      "len(trend):369376 len(df['Symbol']):1867\n",
      "len(trend):371243 len(df['Symbol']):1867\n",
      "len(trend):372871 len(df['Symbol']):1628\n",
      "len(trend):373771 len(df['Symbol']):900\n",
      "len(trend):375637 len(df['Symbol']):1866\n",
      "len(trend):377504 len(df['Symbol']):1867\n",
      "len(trend):379370 len(df['Symbol']):1866\n",
      "len(trend):381236 len(df['Symbol']):1866\n",
      "len(trend):381981 len(df['Symbol']):745\n",
      "len(trend):383847 len(df['Symbol']):1866\n",
      "len(trend):385713 len(df['Symbol']):1866\n",
      "len(trend):387580 len(df['Symbol']):1867\n",
      "len(trend):389447 len(df['Symbol']):1867\n",
      "len(trend):391314 len(df['Symbol']):1867\n",
      "len(trend):391911 len(df['Symbol']):597\n",
      "len(trend):393777 len(df['Symbol']):1866\n",
      "len(trend):395644 len(df['Symbol']):1867\n",
      "len(trend):397511 len(df['Symbol']):1867\n",
      "len(trend):399378 len(df['Symbol']):1867\n",
      "len(trend):401244 len(df['Symbol']):1866\n",
      "len(trend):401884 len(df['Symbol']):640\n",
      "len(trend):403751 len(df['Symbol']):1867\n",
      "len(trend):405618 len(df['Symbol']):1867\n",
      "len(trend):407485 len(df['Symbol']):1867\n",
      "len(trend):409352 len(df['Symbol']):1867\n",
      "len(trend):411219 len(df['Symbol']):1867\n",
      "len(trend):413086 len(df['Symbol']):1867\n",
      "len(trend):414711 len(df['Symbol']):1625\n",
      "len(trend):415194 len(df['Symbol']):483\n",
      "len(trend):417061 len(df['Symbol']):1867\n",
      "len(trend):418927 len(df['Symbol']):1866\n",
      "len(trend):420794 len(df['Symbol']):1867\n",
      "len(trend):422381 len(df['Symbol']):1587\n",
      "len(trend):424248 len(df['Symbol']):1867\n",
      "len(trend):426115 len(df['Symbol']):1867\n",
      "len(trend):427489 len(df['Symbol']):1374\n",
      "len(trend):429356 len(df['Symbol']):1867\n",
      "len(trend):431223 len(df['Symbol']):1867\n",
      "len(trend):433090 len(df['Symbol']):1867\n",
      "len(trend):434957 len(df['Symbol']):1867\n",
      "len(trend):436824 len(df['Symbol']):1867\n",
      "len(trend):438691 len(df['Symbol']):1867\n",
      "len(trend):440557 len(df['Symbol']):1866\n",
      "len(trend):442424 len(df['Symbol']):1867\n",
      "len(trend):444291 len(df['Symbol']):1867\n",
      "len(trend):446158 len(df['Symbol']):1867\n",
      "len(trend):448025 len(df['Symbol']):1867\n",
      "len(trend):449892 len(df['Symbol']):1867\n",
      "len(trend):451758 len(df['Symbol']):1866\n",
      "len(trend):453625 len(df['Symbol']):1867\n",
      "len(trend):455492 len(df['Symbol']):1867\n",
      "len(trend):456905 len(df['Symbol']):1413\n",
      "len(trend):458575 len(df['Symbol']):1670\n",
      "len(trend):460442 len(df['Symbol']):1867\n",
      "len(trend):462309 len(df['Symbol']):1867\n",
      "len(trend):464176 len(df['Symbol']):1867\n",
      "len(trend):466043 len(df['Symbol']):1867\n",
      "len(trend):467909 len(df['Symbol']):1866\n",
      "len(trend):469776 len(df['Symbol']):1867\n",
      "len(trend):471642 len(df['Symbol']):1866\n",
      "len(trend):473509 len(df['Symbol']):1867\n",
      "len(trend):475375 len(df['Symbol']):1866\n",
      "len(trend):477003 len(df['Symbol']):1628\n",
      "len(trend):478870 len(df['Symbol']):1867\n",
      "len(trend):480114 len(df['Symbol']):1244\n",
      "len(trend):480707 len(df['Symbol']):593\n",
      "len(trend):482574 len(df['Symbol']):1867\n",
      "len(trend):484441 len(df['Symbol']):1867\n",
      "len(trend):486308 len(df['Symbol']):1867\n",
      "len(trend):486940 len(df['Symbol']):632\n",
      "len(trend):488807 len(df['Symbol']):1867\n",
      "len(trend):489385 len(df['Symbol']):578\n",
      "len(trend):490384 len(df['Symbol']):999\n",
      "len(trend):490881 len(df['Symbol']):497\n",
      "len(trend):492748 len(df['Symbol']):1867\n",
      "len(trend):494615 len(df['Symbol']):1867\n",
      "len(trend):496111 len(df['Symbol']):1496\n",
      "len(trend):497978 len(df['Symbol']):1867\n",
      "len(trend):499845 len(df['Symbol']):1867\n",
      "len(trend):501711 len(df['Symbol']):1866\n",
      "len(trend):503339 len(df['Symbol']):1628\n",
      "len(trend):505206 len(df['Symbol']):1867\n",
      "len(trend):507073 len(df['Symbol']):1867\n",
      "len(trend):508939 len(df['Symbol']):1866\n",
      "len(trend):510806 len(df['Symbol']):1867\n",
      "len(trend):511376 len(df['Symbol']):570\n",
      "len(trend):512164 len(df['Symbol']):788\n",
      "len(trend):514030 len(df['Symbol']):1866\n",
      "len(trend):514903 len(df['Symbol']):873\n",
      "len(trend):516769 len(df['Symbol']):1866\n",
      "len(trend):518636 len(df['Symbol']):1867\n",
      "len(trend):520503 len(df['Symbol']):1867\n",
      "len(trend):522370 len(df['Symbol']):1867\n",
      "len(trend):522813 len(df['Symbol']):443\n",
      "len(trend):524680 len(df['Symbol']):1867\n",
      "len(trend):526547 len(df['Symbol']):1867\n",
      "len(trend):528414 len(df['Symbol']):1867\n",
      "len(trend):530042 len(df['Symbol']):1628\n",
      "len(trend):531908 len(df['Symbol']):1866\n",
      "len(trend):533773 len(df['Symbol']):1865\n",
      "len(trend):535640 len(df['Symbol']):1867\n",
      "len(trend):537506 len(df['Symbol']):1866\n",
      "len(trend):539134 len(df['Symbol']):1628\n",
      "len(trend):540131 len(df['Symbol']):997\n",
      "len(trend):541128 len(df['Symbol']):997\n",
      "len(trend):542995 len(df['Symbol']):1867\n",
      "len(trend):544862 len(df['Symbol']):1867\n",
      "len(trend):546729 len(df['Symbol']):1867\n",
      "len(trend):548596 len(df['Symbol']):1867\n",
      "len(trend):550462 len(df['Symbol']):1866\n",
      "len(trend):552329 len(df['Symbol']):1867\n",
      "len(trend):554195 len(df['Symbol']):1866\n",
      "len(trend):556061 len(df['Symbol']):1866\n",
      "len(trend):557927 len(df['Symbol']):1866\n",
      "len(trend):559794 len(df['Symbol']):1867\n",
      "len(trend):561660 len(df['Symbol']):1866\n",
      "len(trend):563526 len(df['Symbol']):1866\n",
      "len(trend):564155 len(df['Symbol']):629\n",
      "len(trend):566022 len(df['Symbol']):1867\n",
      "len(trend):567889 len(df['Symbol']):1867\n",
      "len(trend):569756 len(df['Symbol']):1867\n",
      "len(trend):571623 len(df['Symbol']):1867\n",
      "len(trend):573251 len(df['Symbol']):1628\n",
      "len(trend):575118 len(df['Symbol']):1867\n",
      "len(trend):576985 len(df['Symbol']):1867\n",
      "len(trend):578852 len(df['Symbol']):1867\n",
      "len(trend):580718 len(df['Symbol']):1866\n",
      "len(trend):582585 len(df['Symbol']):1867\n",
      "len(trend):584452 len(df['Symbol']):1867\n",
      "len(trend):585209 len(df['Symbol']):757\n",
      "len(trend):587076 len(df['Symbol']):1867\n",
      "len(trend):588943 len(df['Symbol']):1867\n",
      "len(trend):590571 len(df['Symbol']):1628\n",
      "len(trend):591440 len(df['Symbol']):869\n",
      "len(trend):593068 len(df['Symbol']):1628\n",
      "len(trend):594935 len(df['Symbol']):1867\n",
      "len(trend):596229 len(df['Symbol']):1294\n",
      "len(trend):598096 len(df['Symbol']):1867\n",
      "len(trend):599963 len(df['Symbol']):1867\n",
      "len(trend):601830 len(df['Symbol']):1867\n",
      "len(trend):603697 len(df['Symbol']):1867\n",
      "len(trend):604180 len(df['Symbol']):483\n",
      "len(trend):606046 len(df['Symbol']):1866\n",
      "len(trend):606655 len(df['Symbol']):609\n",
      "len(trend):608522 len(df['Symbol']):1867\n",
      "len(trend):610389 len(df['Symbol']):1867\n",
      "len(trend):612256 len(df['Symbol']):1867\n",
      "len(trend):614123 len(df['Symbol']):1867\n",
      "len(trend):615988 len(df['Symbol']):1865\n",
      "len(trend):617855 len(df['Symbol']):1867\n",
      "len(trend):619722 len(df['Symbol']):1867\n",
      "len(trend):621589 len(df['Symbol']):1867\n",
      "len(trend):623456 len(df['Symbol']):1867\n",
      "len(trend):625323 len(df['Symbol']):1867\n",
      "len(trend):627190 len(df['Symbol']):1867\n",
      "len(trend):629057 len(df['Symbol']):1867\n",
      "len(trend):630924 len(df['Symbol']):1867\n",
      "len(trend):632790 len(df['Symbol']):1866\n",
      "len(trend):634657 len(df['Symbol']):1867\n",
      "len(trend):636524 len(df['Symbol']):1867\n",
      "len(trend):638391 len(df['Symbol']):1867\n",
      "len(trend):640257 len(df['Symbol']):1866\n",
      "len(trend):642124 len(df['Symbol']):1867\n",
      "len(trend):643949 len(df['Symbol']):1825\n",
      "len(trend):645575 len(df['Symbol']):1626\n",
      "len(trend):647442 len(df['Symbol']):1867\n",
      "len(trend):649309 len(df['Symbol']):1867\n",
      "len(trend):651176 len(df['Symbol']):1867\n",
      "len(trend):653042 len(df['Symbol']):1866\n",
      "len(trend):654909 len(df['Symbol']):1867\n",
      "len(trend):656776 len(df['Symbol']):1867\n",
      "len(trend):658552 len(df['Symbol']):1776\n",
      "len(trend):660417 len(df['Symbol']):1865\n",
      "len(trend):662284 len(df['Symbol']):1867\n",
      "len(trend):664151 len(df['Symbol']):1867\n",
      "len(trend):666018 len(df['Symbol']):1867\n",
      "len(trend):667884 len(df['Symbol']):1866\n",
      "len(trend):669750 len(df['Symbol']):1866\n",
      "len(trend):671617 len(df['Symbol']):1867\n",
      "len(trend):673484 len(df['Symbol']):1867\n",
      "len(trend):675350 len(df['Symbol']):1866\n",
      "len(trend):677217 len(df['Symbol']):1867\n",
      "len(trend):679084 len(df['Symbol']):1867\n",
      "len(trend):680950 len(df['Symbol']):1866\n",
      "len(trend):681666 len(df['Symbol']):716\n",
      "len(trend):683533 len(df['Symbol']):1867\n",
      "len(trend):685399 len(df['Symbol']):1866\n",
      "len(trend):687266 len(df['Symbol']):1867\n",
      "len(trend):689133 len(df['Symbol']):1867\n",
      "len(trend):691000 len(df['Symbol']):1867\n",
      "len(trend):692867 len(df['Symbol']):1867\n",
      "len(trend):694734 len(df['Symbol']):1867\n",
      "len(trend):696600 len(df['Symbol']):1866\n",
      "len(trend):698467 len(df['Symbol']):1867\n",
      "len(trend):700334 len(df['Symbol']):1867\n",
      "len(trend):702201 len(df['Symbol']):1867\n",
      "len(trend):704068 len(df['Symbol']):1867\n",
      "len(trend):705448 len(df['Symbol']):1380\n",
      "len(trend):707314 len(df['Symbol']):1866\n",
      "len(trend):709181 len(df['Symbol']):1867\n",
      "len(trend):711047 len(df['Symbol']):1866\n",
      "len(trend):712676 len(df['Symbol']):1629\n",
      "len(trend):714543 len(df['Symbol']):1867\n",
      "len(trend):716410 len(df['Symbol']):1867\n",
      "len(trend):718277 len(df['Symbol']):1867\n",
      "len(trend):720143 len(df['Symbol']):1866\n",
      "len(trend):722010 len(df['Symbol']):1867\n",
      "len(trend):722310 len(df['Symbol']):300\n",
      "len(trend):724177 len(df['Symbol']):1867\n",
      "len(trend):726044 len(df['Symbol']):1867\n",
      "len(trend):727911 len(df['Symbol']):1867\n",
      "len(trend):729778 len(df['Symbol']):1867\n",
      "len(trend):731644 len(df['Symbol']):1866\n",
      "len(trend):733511 len(df['Symbol']):1867\n",
      "len(trend):735378 len(df['Symbol']):1867\n",
      "len(trend):737245 len(df['Symbol']):1867\n",
      "len(trend):739112 len(df['Symbol']):1867\n",
      "len(trend):740979 len(df['Symbol']):1867\n",
      "len(trend):742846 len(df['Symbol']):1867\n",
      "len(trend):744713 len(df['Symbol']):1867\n",
      "len(trend):746580 len(df['Symbol']):1867\n",
      "len(trend):748447 len(df['Symbol']):1867\n",
      "len(trend):750314 len(df['Symbol']):1867\n",
      "len(trend):752180 len(df['Symbol']):1866\n",
      "len(trend):754047 len(df['Symbol']):1867\n",
      "len(trend):755914 len(df['Symbol']):1867\n",
      "len(trend):757781 len(df['Symbol']):1867\n",
      "len(trend):759647 len(df['Symbol']):1866\n",
      "len(trend):761513 len(df['Symbol']):1866\n",
      "len(trend):763377 len(df['Symbol']):1864\n",
      "len(trend):765244 len(df['Symbol']):1867\n",
      "len(trend):767111 len(df['Symbol']):1867\n",
      "len(trend):768978 len(df['Symbol']):1867\n",
      "len(trend):769588 len(df['Symbol']):610\n",
      "len(trend):771454 len(df['Symbol']):1866\n",
      "len(trend):773321 len(df['Symbol']):1867\n",
      "len(trend):774950 len(df['Symbol']):1629\n",
      "len(trend):776816 len(df['Symbol']):1866\n",
      "len(trend):778683 len(df['Symbol']):1867\n",
      "len(trend):780549 len(df['Symbol']):1866\n",
      "len(trend):782416 len(df['Symbol']):1867\n",
      "len(trend):784283 len(df['Symbol']):1867\n",
      "len(trend):784773 len(df['Symbol']):490\n",
      "len(trend):786639 len(df['Symbol']):1866\n",
      "len(trend):788506 len(df['Symbol']):1867\n",
      "len(trend):790373 len(df['Symbol']):1867\n",
      "len(trend):792239 len(df['Symbol']):1866\n",
      "len(trend):794106 len(df['Symbol']):1867\n",
      "len(trend):795973 len(df['Symbol']):1867\n",
      "len(trend):796190 len(df['Symbol']):217\n",
      "len(trend):798056 len(df['Symbol']):1866\n",
      "len(trend):799923 len(df['Symbol']):1867\n",
      "len(trend):801789 len(df['Symbol']):1866\n",
      "len(trend):803656 len(df['Symbol']):1867\n",
      "len(trend):805074 len(df['Symbol']):1418\n",
      "len(trend):806940 len(df['Symbol']):1866\n",
      "len(trend):808807 len(df['Symbol']):1867\n",
      "len(trend):810674 len(df['Symbol']):1867\n",
      "len(trend):812540 len(df['Symbol']):1866\n",
      "len(trend):813632 len(df['Symbol']):1092\n",
      "len(trend):813632 len(df):813632\n",
      "Date detrend: Done\n"
     ]
    }
   ],
   "source": [
    "data = detrend_data(data)\n",
    "print(\"Date detrend: Done\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = calculate_targets(data, 1)\n",
    "data = calculate_targets(data, 3)\n",
    "data = calculate_targets(data, 5)\n",
    "data = calculate_targets(data, 10)\n",
    "data = calculate_targets(data, 14)\n",
    "data = calculate_targets(data, 30)\n",
    "print('Targets Done - except 60')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data.to_csv(\"./data_preprocessed.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data_processed = pd.read_csv(\"data_preprocessed.csv\")\n",
    "print(data_processed[:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "stock= data_processed['Symbol'].unique()[0]\n",
    "plt.figure(figsize=(10,10))\n",
    "\n",
    "plt.plot(data_processed[data_processed[\"Symbol\"]==stock][\"WR\"],label=\"WR\")\n",
    "plt.plot(data_processed[data_processed[\"Symbol\"]==stock][\"SO\"],label=\"So\")\n",
    "plt.plot(data_processed[data_processed[\"Symbol\"]==stock][\"RSI\"],label=\"RSI\")\n",
    "plt.plot(data_processed[data_processed[\"Symbol\"]==stock][\"PROC\"],label=\"PROC\")\n",
    "\n",
    "plt.plot(data_processed[data_processed[\"Symbol\"]==stock][\"Close\"],label=\"Close\")\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
